from django.db import models
from django.db.models import permalink

import datetime
import dateutil


class PersonType(models.Model):
  """ Person Type model """
  title = models.CharField(maxlength=100)
  slug = models.SlugField(prepopulate_from=("title",))
  
  class Meta:
    db_table = "people_types"
    ordering = ("title",)

  class Admin:
    pass

  def __str__(self):
    return '%s' % self.title

  @permalink
  def get_absolute_url(self):
    return ('person_type_detail', None, { 'slug': self.slug })


class Person(models.Model):
  """ Person model """
  GENDER_CHOICES = (
    (1, 'Male'),
    (2, 'Female'),
  )
  first_name        = models.CharField(blank=True, maxlength=100)
  middle_name       = models.CharField(blank=True, maxlength=100)
  last_name         = models.CharField(blank=True, maxlength=100)
  slug              = models.SlugField(prepopulate_from=('first_name','last_name',))
  gender            = models.PositiveSmallIntegerField(choices=GENDER_CHOICES, blank=True, null=True)
  mugshot           = models.FileField(upload_to='mugshots', blank=True)
  birth_date        = models.DateField(blank=True, null=True)
  person_types      = models.ManyToManyField(PersonType, blank=True)
  website           = models.URLField(blank=True, verify_exists=True)
  
  class Meta:
    verbose_name_plural = "people"
    db_table = "people"
    ordering = ("last_name", "first_name",)

  class Admin:
    pass

  def __str__(self):
    return '%s' % self.full_name
  
  @property
  def full_name(self):
    return "%s %s" % (self.first_name, self.last_name)
  
  @property
  def age(self):
    TODAY = datetime.date.today()
    return '%s' % dateutil.relativedelta(TODAY, self.birth_date).years
  
  @permalink
  def get_absolute_url(self):
    return ('person_detail', None, { 'slug': self.slug })